package com.qinggeng.project.system.service.impl;

import cn.hutool.core.date.DateUtil;
import com.qinggeng.common.exception.CustomException;
import com.qinggeng.common.utils.DateUtils;
import com.qinggeng.common.utils.StringUtils;
import com.qinggeng.project.system.domain.OrdersArerRecord;
import com.qinggeng.project.system.mapper.OrdersArerRecordMapper;
import com.qinggeng.project.system.service.OrdersArerRecordService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class OrdersArerRecordServiceImpl implements OrdersArerRecordService {
    @Resource
    private OrdersArerRecordMapper ordersArerRecordMapper;
    @Override
    public boolean insertOrdersArerRecord(OrdersArerRecord ordersArerRecord) {
        String areaName=ordersArerRecord.getAreaName();
        if (StringUtils.isEmpty(areaName)){
            throw  new CustomException("区域不能为空");
        }
        String recordDate=ordersArerRecord.getRecordDate();

        if (StringUtils.isEmpty(recordDate) ){
            throw  new CustomException("recordDate is  null");
        }
        Example example =new Example(OrdersArerRecord.class);
        example.createCriteria()
                .andEqualTo("areaName",areaName)
                .andEqualTo("recordDate",recordDate);
        OrdersArerRecord orders=ordersArerRecordMapper.selectOneByExample(example);
        if (orders!=null){
            return  true;
        }
        //ordersArerRecord.setRecordDate(recordDate);
        ordersArerRecord.setRecordTime(DateUtils.getNowDate());
        ordersArerRecord.setCreateTime(DateUtils.getNowDate());
        return ordersArerRecordMapper.insertSelective(ordersArerRecord)>0;
    }

    public void insertOrdersAreaRecord(String areaName,String recordDate){
        OrdersArerRecord ordersArerRecord=new OrdersArerRecord();
        ordersArerRecord.setAreaName(areaName);
        ordersArerRecord.setRecordDate(recordDate);
        if(!this.insertOrdersArerRecord(ordersArerRecord)){
            log.error("新增订单区域统计失败");
            throw new CustomException("新增订单区域统计失败");
        }
    }

    @Override
    public List<String> listOrdersArerRecordName(String recordDate) {
        Example example =new Example(OrdersArerRecord.class);
        example.createCriteria().andEqualTo("recordDate",recordDate);
        List<OrdersArerRecord> ordersArerRecordList=ordersArerRecordMapper.selectByExample(example);
        List<String> stringArrayList=new ArrayList<>();
        for (OrdersArerRecord ordersArerRecord : ordersArerRecordList) {
            stringArrayList.add(ordersArerRecord.getAreaName());
        }
        return stringArrayList;
    }


}
